package com.hulk.dryad.admin.controller;

import com.hulk.dryad.admin.api.vo.SysPostVO;
import com.hulk.dryad.admin.mapstruct.SysPostStruct;

import com.hulk.dryad.admin.controller.basic.IController;

import com.hulk.dryad.common.constant.enums.BEC;
import com.hulk.dryad.persistence.entity.SysPost;
import com.hulk.dryad.admin.service.SysPostService;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.annotation.OperLog;
import com.hulk.dryad.common.constant.enums.ActionType;
import com.hulk.dryad.common.annotation.Idempotent;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;


/**
 * 岗位信息
 *
 * @author hulk
 * @date 2021-04-01 10:06:32
 */
@Api(value = "admin_syspost", tags = "岗位信息管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/syspost" )
public class SysPostController implements IController{

    private final  SysPostService sysPostService;

    /**
     * 分页查询岗位信息
     * @param page 分页对象
     * @param vo 实体
     * @return Page
     */
    @ApiOperation(value = "分页查询岗位信息", notes = "分页查询岗位信息")
    @PrintlnLog
    @GetMapping("/page" )
    public R<IPage> getSysPostPage(Page page, SysPostVO vo) {
        SysPost model = SysPostStruct.INSTANCE.to(vo);
        Page<SysPost> rpm =  sysPostService.page(page, Wrappers.query(model));
        Page<SysPostVO>  rpv = SysPostStruct.INSTANCE.from(rpm);
        return R.ok(rpv);
    }

    /**
     * 通过id查询岗位详情
     * @param postId ID
     * @return SysPost
     */
    @ApiOperation(value = "通过id查询岗位详情", notes = "通过id查询岗位详情")
    @PrintlnLog
    @GetMapping("/{postId}" )
    public R getById(@PathVariable("postId" ) Long postId) {
        SysPost model = sysPostService.getById(postId);
        SysPostVO vo = SysPostStruct.INSTANCE.from(model);
        return R.ok(vo);
    }
	/**
	 * 验证岗位名称是否存在
	 * @param name 岗位名
	 * @return count
	 */
	@ApiOperation(value = "checkByName", notes = "验证岗位名称是否存在")
	@PrintlnLog
	@GetMapping("/name/{name}")
	public R checkByName(@PathVariable String name) {
		int count =sysPostService.count(Wrappers.<SysPost>lambdaQuery().eq(SysPost::getName,name));
		if(count>0){
			return R.failed(BEC.E_1006,count);
		}
		return R.ok(count);
	}
	/**
	 * 验证岗位编码是否存在
	 * @param code 岗位编码
	 * @return count
	 */
	@ApiOperation(value = "checkByCode", notes = "验证岗位编码是否存在")
	@PrintlnLog
	@GetMapping("/code/{code}")
	public R checkByCode(@PathVariable String code) {
		int count =sysPostService.count(Wrappers.<SysPost>lambdaQuery().eq(SysPost::getCode,code));
		if(count>0){
			return R.failed(BEC.E_1006,count);
		}
		return R.ok(count);
	}
    /**
     * 新增岗位信息
     * @param vo 实体
     * @return SysPost
     */
    @ApiOperation(value = "新增岗位信息", notes = "新增岗位信息")
    @OperLog(value = "新增岗位信息", action = ActionType.ADD)
    @Idempotent
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_syspost_add')" )
    public R save(@Valid @RequestBody SysPostVO vo) {
        SysPost model = SysPostStruct.INSTANCE.to(vo);
//		int count =sysPostService.count(Wrappers.<SysPost>lambdaQuery().eq(SysPost::getDelFlag, DelFlag.N.getValue())
//				.and(v-> v.eq(SysPost::getName,model.getName())
//						.or().eq(SysPost::getCode,model.getCode())));
//		if(count>0){
//			return R.failed(BEC.E_1006);
//		}
        return R.ok(sysPostService.save(model));
    }

    /**
     * 通过id修改岗位信息
     * @param vo 岗位信息
     * @return boolean
     */
    @ApiOperation(value = "通过id修改岗位信息", notes = "通过id修改岗位信息")
    @OperLog(value ="通过id修改岗位信息", action = ActionType.UPDATE)
    @Idempotent
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_syspost_edit')" )
    public R updateById(@Valid @RequestBody SysPostVO vo) {
        SysPost model = SysPostStruct.INSTANCE.to(vo);
        return R.ok(sysPostService.updateById(model));
    }

    /**
     * 通过id删除岗位信息
     * @param postId ID
     * @return boolean
     */
    @ApiOperation(value = "通过id删除岗位信息", notes = "通过id删除岗位信息")
    @OperLog(value = "通过id删除岗位信息", action = ActionType.DEL)
	@DeleteMapping("/{postId}" )
    @PreAuthorize("@pms.hasPermission('admin_syspost_del')" )
    public R removeById(@PathVariable Long postId) {
        return R.ok(sysPostService.removeById(postId));
    }



	/**
	 * 获取岗位列表
	 * @return 岗位列表
	 */
	@ApiOperation(value = "listPosts", notes = "获取岗位列表")
	@PrintlnLog
	@GetMapping("/list")
	public R getAllPosts() {
		List<SysPost> models =	sysPostService.getAllPosts();
		List<SysPostVO> vos =	SysPostStruct.INSTANCE.from(models);
		return R.ok(vos);
	}
}
